package com.dckj.auto.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dckj.auto.common.utils.StringUtil;
import com.dckj.auto.entity.AuthMenu;
import com.dckj.auto.dao.AuthMenuDao;
import com.dckj.auto.entity.AuthMenuRole;
import com.dckj.auto.service.AuthMenuRoleService;
import com.dckj.auto.service.AuthMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dckj.auto.service.AuthUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WangDong123
 * @since 2018-09-03
 */
@Service
public class AuthMenuServiceImpl extends ServiceImpl<AuthMenuDao, AuthMenu> implements AuthMenuService {

    @Autowired
    AuthMenuDao autoMenuDao;
    @Autowired
    AuthMenuRoleService autoMenuRoleService;
    @Autowired
    AuthUserRoleService userRoleService;

    @Override
    public Integer delete(String id) {
        Integer integer = autoMenuDao.deleteById(id);
        if(integer<=0) {
            return integer;
        }
        List<String> ids = ids(id);
        if(ids.size()>0){
            autoMenuDao.deleteBatchIds(ids);
            autoMenuRoleService.delete(new EntityWrapper<AuthMenuRole>().in("menu_id",ids));
        }
        return integer;
    }

    @Override
    public Integer saveOrUpdate(AuthMenu autoMenu) {
        if("1".equals(autoMenu.getSpread())){
            autoMenu.setSpread("true");
        }else{
            autoMenu.setSpread("false");
        }
        if(StringUtils.isEmpty(autoMenu.getMenuId())){
            autoMenu.setMenuId(StringUtil.getUUId());
            return autoMenuDao.insert(autoMenu);
        }
        return autoMenuDao.updateById(autoMenu);
    }

    @Override
    public List<AuthMenu> list() {
        AuthMenu auto = new AuthMenu();auto.setFatherId("0");
        AuthMenu auto1 = autoMenuDao.selectOne(auto);
        return list(auto1.getAuthorityId());
    }

    @Override
    public List<AuthMenu> list(String fatherId) {
        EntityWrapper<AuthMenu> en = new EntityWrapper<>();
        en.eq("father_id",fatherId);
        return autoMenuDao.selectList(en);
    }

    @Override
    public List<AuthMenu> list(Integer type) {
        EntityWrapper<AuthMenu> en = new EntityWrapper<>();
        en.eq("attribute",type);
        en.orderBy("num+authority_id");
        return  autoMenuDao.selectList(en);
    }

    @Override
    public List<String> ids(String id) {
        List<String> ids = new ArrayList<>();
        List<String> delIds = new ArrayList<>();
        for (int i=0 ; i<i+1; i++){
            if(i==0){
                delIds.add(id);
            }
            List<AuthMenu> autoMenus = autoMenuDao.selectList(new EntityWrapper<AuthMenu>().in("father_id",delIds));
            if(autoMenus.size()<=0){
                break;
            }
            delIds =  new ArrayList<>();
            for (AuthMenu menus : autoMenus){
                delIds.add(menus.getMenuId());
            }
            ids.addAll(delIds);
        }
        return ids;
    }

}
